package com.yx.cloud.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.GraphicspasswordActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.yx.cloud.view.LockView;
import java.lang.ref.SoftReference;
import static com.iot.common.utils.Event.MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD;

public class GraphicsPasswordActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "GraphicsPasswordActivity";
    public static final int SUCCESS = 0;
    public static final int FAILURE = -1;
    public static final int RESET = -2;
    private GraphicspasswordActivityBinding binding;
    private LockView lockview;
    private Handler handler;
    private Intent intent;
    private String tag;//set->设置密码；close->关闭密码
    private int step = 1;
    private Dialog dialog;

    @Override
    protected void onCreate() {
        binding = GraphicspasswordActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        intent = getIntent();
        tag = intent.getStringExtra("tag");
        handler = new GraphicPasswordHandler(this);
        lockview = findViewById(R.id.lock_view);

        //添加密码检验回调函数
        lockview.addCheckPasswordCallBack(new LockView.CheckPasswordCallBack() {
            @Override
            public void onFailure() {
                changeViewStatus(tag,step,FAILURE);
            }

            @Override
            public void onSuccess() {
                changeViewStatus(tag,step,SUCCESS);
            }

            @Override
            public void onReset() {
                changeViewStatus(tag,step,RESET);
            }
        });

        initView(tag);
        initData(tag);
    }

    private void initView(String tag){
        if(tag.equals("set")){
            binding.tvGraphicPasswordTitle.setText(getString(R.string.U_Set_Psw));
            binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
            binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des2));
            binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
            binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
            binding.tvGraphicForgetPasswordTips.setVisibility(View.INVISIBLE);
        }else if(tag.equals("close")){
            binding.tvGraphicPasswordTitle.setText(getString(R.string.U_Colse_Psw));
            binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
            binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
            binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
            binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
            binding.tvGraphicForgetPasswordTips.setVisibility(View.VISIBLE);
        }else if(tag.equals("modify")){
            binding.tvGraphicPasswordTitle.setText(getString(R.string.U_Pw_Edit));
            binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
            binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
            binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
            binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
            binding.tvGraphicForgetPasswordTips.setVisibility(View.INVISIBLE);
        } else if(tag.equals("verify")){
            binding.tvGraphicPasswordTitle.setText(getString(R.string.U_Check_Psw));
            binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
            binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
            binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
            binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
            binding.tvGraphicForgetPasswordTips.setVisibility(View.INVISIBLE);
        }
        binding.ivGraphicPasswordReturn.setOnClickListener(this);
        binding.tvGraphicForgetPasswordTips.setOnClickListener(this);
    }

    private void initData(String tag){
        if(tag.equals("set")){
            lockview.setPass("");
        }else if(tag.equals("close") || tag.equals("modify") || tag.equals("verify")) {
            String password = AppContext.get().getCurrentDevice().getCameraSettings().getGraphicPassword();
            lockview.setPass(password);//设置dp点获取到的密码

        }
    }

    private void changeViewStatus(String tag,int step,int status){
        if(tag.equals("set")){
            if(status == SUCCESS){
                if(step == 2){
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setVideoPassword(lockview.getPass()).build());
                    finish();
                }else if(step == 1){
                    this.step = 2;
                    binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip3));
                    handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
                }
            }else if(status == FAILURE){
                if(lockview.getPass().equals("")){
                    binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                    binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des2));
                    binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                    binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                }else{
                    binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des3));
                    binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.restrict_area_stroke));
                    binding.tvGraphicPasswordTipsTitle.setVisibility(View.INVISIBLE);
                }
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
            }else if(status == RESET){
                if(step == 2){
                    binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                    binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des2));
                    binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip3));
                    binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                    binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                }else if(step == 1){
                    binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                    binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des2));
                    binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                    binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                    binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                }
            }
        }else if(tag.equals("close")){
            if(status == SUCCESS){
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setLiveVideoSwitch(false).build());
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setVideoPassword("").build());
                ToastUtil.show(this, getString(R.string.graphic_password_close_password_success), Toast.LENGTH_LONG);
                finish();
            }else if(status == FAILURE){
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des3));
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.restrict_area_stroke));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.INVISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
            }else if(status == RESET){
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
                binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
                binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
            }
        }else if(tag.equals("modify")){
            if(status == SUCCESS){
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des2));
                binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
                binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                this.tag = "set";
                lockview.setPass("");
                handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
            }else if(status == FAILURE){
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des3));
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.restrict_area_stroke));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.INVISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
            }else if(status == RESET){
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
                binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
                binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
            }
        }else if(tag.equals("verify")){
            if(status == SUCCESS){
                Intent_utils.enterIntent(this, LiveVideoActivity.class);
                finish();
            }else if(status == FAILURE){
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des3));
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.restrict_area_stroke));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.INVISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD,500);
            }else if(status == RESET){
                binding.tvGraphicPasswordTips.setTextColor(getColor(R.color.textview_color1));
                binding.tvGraphicPasswordTips.setText(getString(R.string.U_9P_Psw_Des1));
                binding.tvGraphicPasswordTipsTitle.setTextColor(getColor(R.color.textview_color2));
                binding.tvGraphicPasswordTipsTitle.setText(getString(R.string.U_9P_Psw_Tip1));
                binding.tvGraphicPasswordTipsTitle.setVisibility(View.VISIBLE);
                binding.tvGraphicPasswordTips.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_graphic_password_Return:
                finish();
                break;
            case R.id.tv_graphic_forget_password_tips:
                forgetPasswordDialog(this,getString(R.string.U_9P_Psw_Forget_Tip),  getString(R.string.L_Pw_Forget), getString(R.string.U_Cancel_Btn), getString(R.string.U_Ok_Btn));
                break;
        }
    }

    /**
     * 新版本Dialog
     */
    public void forgetPasswordDialog(Context context, String content, String
            dialogTitle, String str1, String str2) {
        if (dialog == null) {
            dialog = new Dialog(context);
        }
        if (dialog.isShowing()) {
            return;
        }
        dialog.setContentView(R.layout.outlogin_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        TextView mBtnOutLoginConfirm = dialog.findViewById(R.id.btn_outLoginConfirm);
        TextView mBtnOutLoginCancel = dialog.findViewById(R.id.btn_outLoginCancel);
        TextView mTvDialogTitle = dialog.findViewById(R.id.tv_dialogTitle);
        TextView mContent = dialog.findViewById(R.id.tv_content);

        mTvDialogTitle.setText(dialogTitle);
        mContent.setText(content);

        mBtnOutLoginCancel.setText(str1);
        mBtnOutLoginConfirm.setText(str2);
        mBtnOutLoginConfirm.setTextColor(getResources().getColor(R.color.button_color2));

        mBtnOutLoginConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭密码
                //AppContext.get().getCurrentDevice().getCameraSettings().setGraphicPassword("");//test
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setVideoPassword("").build());
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setLiveVideoSwitch(false).build());
                dialog.dismiss();
                finish();
            }
        });
        mBtnOutLoginCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    public static class GraphicPasswordHandler extends Handler {
        SoftReference<GraphicsPasswordActivity> graphicPasswordHandlerSoftReference;

        public GraphicPasswordHandler(GraphicsPasswordActivity activity) {
            graphicPasswordHandlerSoftReference = new SoftReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MESSAGE_TYPE_RESET_GRAPHIC_PASSWORD) {
                graphicPasswordHandlerSoftReference.get().lockview.resetData();
                graphicPasswordHandlerSoftReference.get().lockview.invalidate();
            }
        }
    }
}
